package com.tping.thread;

import java.util.concurrent.*;
import java.util.stream.IntStream;

/**
 * 简单使用多线程
 *
 * 1、继承Thread
 * 2、实现Runnable接口
 * 3、实现Callable接口
 */
public class Demo {


    public static void main(String[] args){
        // 继承Thread方式实现
        MyThread thread = new MyThread();
        thread.start();

        // 实现Runnable接口
        new Thread(new MyRunnable()).start();

        // 匿名线程
        new Thread(() ->{
            System.out.println("Java 8 匿名内部类");
        }).start();


        // 通过实现Callable接口实现线程执行完返回结果值
        ExecutorService executor = Executors.newCachedThreadPool();
        MyCallable callable = new MyCallable();
        Future<Integer> future = executor.submit(callable);

        FutureTask<Integer> futureTask = new FutureTask<Integer>(callable);

        executor.submit(futureTask);
        try {
            Integer result = future.get();

            System.out.println(result);

            System.out.println(futureTask.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 线程优先级
        IntStream.range(1, 10).forEach(i -> {
            MyThread myThread = new MyThread();
            myThread.setPriority(i);
            myThread.start();
        });

        /**
         * 线程优先级
         * 线程组优先级
         */
        ThreadGroup threadGroup = new ThreadGroup("thread");
        threadGroup.setMaxPriority(8);  // 设置线程组最大优先级为：8
        Thread thread1 = new Thread(threadGroup, "thread1");
        thread1.setPriority(9);

        /**
         * 如果某个线程优先级大于线程所在线程组的最大优先级，
         * 那么该线程的优先 级将会失效，取而代之的是线程组的最大优先级
         */
        System.out.println("线程组优先级：" + threadGroup.getMaxPriority());
        System.out.println("线程优先级：" + thread1.getPriority());
    }

    private static class MyThread extends Thread{

        @Override
        public void run() {
            System.out.println("继承 Thread 类实现多线程...");

            System.out.println(String.format("当前执行线程是：%s, 线程优先级：%s", Thread.currentThread().getName(), Thread.currentThread().getPriority()));
        }
    }

    private static class MyRunnable implements Runnable{

        @Override
        public void run() {
            System.out.println("实现 Runnable 接口实现多线程...");
        }
    }

    /**
     * 有返回值的线程
     */
    private static class MyCallable implements Callable<Integer>{

        @Override
        public Integer call() throws Exception {
            return 2021;
        }
    }



    protected void copyThreadGroup(){
        // 获取线程组名称
        System.out.println(Thread.currentThread().getThreadGroup().getName());

        ThreadGroup threadGroup = new ThreadGroup("threadGroup");

        // 线程组里的线程个数
        // 复制一个线程数组到一个线程组中
        Thread[] threads = new Thread[threadGroup.activeCount()];
        ThreadGroup newThreadGroup = new ThreadGroup("newThreadGroup");
        newThreadGroup.enumerate(threads);

    }
}
